package com.zondy.mapgis.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


public class FileOperationUtils
{

	/**
	 * 读文件的得到文件字节流
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] readFileToBytes(String filePath)
	{
		byte[] bytes = null;
		try
		{
			BufferedInputStream bi = new BufferedInputStream(new FileInputStream(filePath));
			// 输入流转字节流
			bytes = new byte[bi.available()];
			bi.read(bytes);
			bi.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return bytes;

	}

	/**
	 * 把字节流放到本地
	 * 
	 * @param bytes
	 * @param filePath
	 * @throws IOException
	 */
	public static void writeBytesFile(byte[] bytes, String filePath)
	{
		try
		{
			BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(filePath));
			bo.write(bytes);
			bo.flush();
			bo.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 文件输入流写入到本地
	 * 
	 * @param in
	 * @param filePath
	 */
	public static void writeInputStreamFile(InputStream in, String filePath)
	{
		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
		BufferedOutputStream bo;
		byte[] buffer = new byte[1024];
		int ch;
		try
		{
			while ((ch = in.read(buffer)) != -1)
			{
				bytestream.write(buffer, 0, ch);
			}
			byte[] data = bytestream.toByteArray();

			// 判断父文件夹是否被创建
			File Fout = new File(filePath);
			if (!Fout.exists())
			{
				(new File(Fout.getParent())).mkdirs();
			}

			bo = new BufferedOutputStream(new FileOutputStream(filePath));
			bo.write(data);
			bo.flush();
			bo.close();
			bytestream.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 读输入流得到文件字节流
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] readInputStreamToBytes(InputStream in)
	{
		byte[] bytes = null;
		try
		{
			BufferedInputStream bi = new BufferedInputStream(in);
			// 输入流转字节流
			bytes = new byte[bi.available()];
			bi.read(bytes);
			bi.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return bytes;

	}

	/**
	 * 输入流转字节流
	 * */
	public static byte[] InputStreamToByte(InputStream is) throws IOException
	{
		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int ch;
		while ((ch = is.read(buffer)) != -1)
		{
			bytestream.write(buffer, 0, ch);
		}
		byte data[] = bytestream.toByteArray();
		bytestream.close();
		return data;

	}

	// 按日期排序
	public static List<File> orderByDate(List<File> flieLst)
	{
		List<File> orderFileLst = new ArrayList<File>();
		File[] fs = new File[flieLst.size()];
		flieLst.toArray(fs);
		Arrays.sort(fs, new Comparator<File>()
		{
			public int compare(File f1, File f2)
			{
				long diff = f1.lastModified() - f2.lastModified();
				if (diff > 0)
					return 1;
				else if (diff == 0)
					return 0;
				else
					return -1;
			}

			public boolean equals(Object obj)
			{
				return true;
			}

		});
		for (int i = fs.length - 1; i > -1; i--)
		{
			orderFileLst.add(fs[i]);
		}
		return orderFileLst;
	}



	/**
	 * 获取系统当前时间
	 * 
	 * @return
	 */
	public static String getCurrentTime()
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(System.currentTimeMillis());
		return dateFormat.format(date);
	}

	/**
	 * 将字符串追加到文件中
	 * 
	 * @param fileFullPath 文件完整地址
	 * @param content 需要写入的
	 */
	public static void writeFile(String fileFullPath, String content)
	{
		FileOutputStream fos = null;
		File Fout = new File(fileFullPath);
		if (!Fout.exists())
		{
			(new File(Fout.getParent())).mkdirs();
		}
		try
		{
			fos = new FileOutputStream(fileFullPath, true);
			// 写入
			fos.write(content.getBytes());
			// 写入一个换行
			fos.write("\r\n".getBytes());

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (fos != null)
			{
				try
				{
					fos.flush();
					fos.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

}
